package ve.ucv.patologiaocular.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import ve.ucv.patologiaocular.domain.Foto;

public class FotoDAOImpl implements FotoDAO {
	private EntityManagerFactory entityManagerFactory;
	private EntityManager entityManager;

	public FotoDAOImpl() {
		this.entityManagerFactory = Persistence
				.createEntityManagerFactory("patologiads");
		this.entityManager = entityManagerFactory.createEntityManager();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Foto> listarFotos() {
		Query q = entityManager.createQuery("select imagen from Foto imagen");
		return q.getResultList();

	}

	@Override
	public void guardar(Foto foto) {
		entityManager.getTransaction().begin();
		entityManager.persist(foto);
		entityManager.flush();
		entityManager.getTransaction().commit();
	}
	
	@Override
	public void editar(Foto foto) {
		entityManager.getTransaction().begin();
		entityManager.merge(foto);
		entityManager.flush();
		entityManager.getTransaction().commit();
	}

	@Override
	public void borrar(Foto foto) {
		entityManager.getTransaction().begin();
		entityManager.remove(foto);
		entityManager.flush();
		entityManager.getTransaction().commit();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	@Override
	public Foto buscar(Integer id) {
		return entityManager.find(Foto.class, id);

	}
	
/*	public List<Foto> testGetBook() {

		Book book = bookDAO.getBook((long) 1);

 

assertNotNull(book);

 

.try{

//FileOutputStream fos = new FileOutputStream("images\\output.jpg");  //windows

FileOutputStream fos = new FileOutputStream("images/output.jpg");

fos.write(book.getImage());

fos.close();

}catch(Exception e){

e.printStackTrace();

}

}*/

}
